import 'package:flutter/material.dart';

class TestWidgetSize extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyState();
  }
}

class _MyState extends State<TestWidgetSize> {
  GlobalKey _myKey = new GlobalKey();
  bool _isNeedShow = false;

  List<ListItem> _itemDatas = [];

  @override
  void initState() {
    super.initState();

    _itemDatas.add(ListItem('获取大小1', GlobalKey(), 10.0));
    _itemDatas.add(ListItem('获取大小2', GlobalKey(), 12.0));
    _itemDatas.add(ListItem('获取大小3', GlobalKey(), 15.0));
    _itemDatas.add(ListItem('获取大小4', GlobalKey(), 20.0));
    _itemDatas.add(ListItem('获取大小5', GlobalKey(), 32.0));
  }

  @override
  Widget build(BuildContext context) {

    if (_isNeedShow) {
    }
    return new Scaffold(
        appBar: new AppBar(),
        body: new Column(
          children: <Widget>[
//            new Container(
//              key:_myKey,
//              color:Colors.black12,
//              child: new Column(
//                mainAxisSize: MainAxisSize.min,
//                children: items
//              ),
//            ),
            Expanded(
              child: Container(
                key: _myKey,
                color: Colors.black12,
                child: ListView(
                  children: _itemDatas.map((item) => Text(item.txt, key:item.key, style: TextStyle(fontSize: item.textSize))).toList(),
                ),
              ),
            ),
            Padding(
              padding: new EdgeInsets.only(top: 100.0),
              child: new RaisedButton(
                onPressed: () {
                  setState(() {
                    _isNeedShow = !_isNeedShow;
                  });
                },
                child: new Text("获取大小"),
              ),
            ),
            Wrap(
              spacing: 8.0, // 主轴(水平)方向间距
              runSpacing: 4.0, // 纵轴（垂直）方向间距
              alignment: WrapAlignment.start, //沿主轴方向居中
              children: <Widget>[
                new Chip(
                  avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('A')),
                  label: new Text('Hamilton'),
                ),
                new Chip(
                  avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('M')),
                  label: new Text('Lafayette'),
                ),
                new Chip(
                  avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('H')),
                  label: new Text('Mulligan'),
                ),
                new Chip(
                  avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('J')),
                  label: new Text('Laurens'),
                ),
              ],
            )
          ],
        ));
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    WidgetsBinding.instance.addPostFrameCallback(onAfterRendering);
  }

  @override
  void didUpdateWidget(TestWidgetSize oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    WidgetsBinding.instance.addPostFrameCallback(onAfterRendering);
  }

  void onAfterRendering(Duration duration) {
    print('----------------complete--------------${duration.toString()}');
    _itemDatas.forEach((item) {
      RenderObject renderObject = item.key.currentContext.findRenderObject();
      print("semanticBounds:${renderObject.semanticBounds.size} paintBounds:${renderObject.paintBounds.size} size:${item.key.currentContext.size}");
    });
  }
}

class ListItem {
  String txt;
  GlobalKey key;
  double textSize;

  ListItem(this.txt, this.key, this.textSize);
}
